/*
 * Copyright (c) 2020. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package com.evenmo.string;

import java.util.concurrent.TimeUnit;

/**
 * This is Description ThreadLocal 线程局部变量
 *
 * @author moyongjun
 * @date 2020/01/05
 */
public class Thread_ThreadLocal {

    static ThreadLocal<Person> tl=new ThreadLocal<Person>();


    public static void main(String[] args) {

        new Thread(()->{
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Person person = tl.get();
            System.out.println("t1:"+person);
            tl.set(new Person());
            person=tl.get();
            person.name="hello";


            System.out.println("t1 set后："+tl.get());
        }).start();

        new Thread(()->{
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            tl.set(new Person());
            Person person = tl.get();
            System.out.println("t2:"+person);
        }).start();
    }

}

class Person{
    String name="张三";
    public String toString(){
        return "name="+name;

    }
}